<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class MusicStylesController extends AppController {

    var $name = 'MusicStyle';
    var $components = array(
        'Session', 'Alias'
    );
    var $helpers = array(
        'Editor', 'Published'
    );
    var $uses = array(
        'music_style'
    );
    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' - Thể loại nhạc');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'title LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'music_style.id' => 'DESC'
            ),
        );

        $data = $this->paginate('music_style');
        $this->set('total', $this->params['paging']['music_style']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }

    function remove() {
        $conditions = array(
            "{$this->Singer->alias}.{$this->Singer->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );
        if ($this->Singer->deleteAll($conditions)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d ca sỹ", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }

    function edit() {
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->music_style->id = $params['cid'][0];
        } else {
            $this->music_style->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }

        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');

        if (empty($this->data)) {
            $fields = array(
                '*'
            );
            $this->data = $this->music_style->read($fields);
            $this->set('description', $this->data['music_style']['description']);
        }
    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Thêm thể loại nhạc');
    }
    function save() {
        $data = $this->data['music_style'];

        $data['alias'] = $this->Alias->alias($data['title']);
        if ($this->music_style->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->music_style->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'music_styles', 'action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('controller' => 'music_styles', 'action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
            );
        }

        if ($this->music_style->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->music_style->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'music_styles', 'action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
            );
        }

        if ($this->music_style->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->music_style->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'music_styles', 'action' => 'index'));
    }

}

?>
